import json
from docx import Document
from docx.shared import Pt, Cm
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
import requests

from flask import Flask, request, jsonify, send_from_directory
import uuid
import os
import shutil
from Utils.doc_parser import parse_document
from Utils.llm_client import build_deepseek_prompt

import json
import os

from PyPDF2 import PdfReader, PdfWriter
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
import io

def save_analysis_data(file_id, data):  # 保存分析结果
    """保存分析结果到文本文件"""
    # 创建存储目录（如果不存在）
    os.makedirs('analysis_data', exist_ok=True)

    # 构建文件路径
    filepath = os.path.join('analysis_data', f'{file_id}.json')

    try:
        with open(filepath, 'w', encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=2)
        return True
    except Exception as e:
        print(f"保存失败: {str(e)}")
        return False


def get_analysis_data(file_id):  # 读取保存数据
    """从文本文件加载分析结果"""
    filepath = os.path.join('analysis_data', f'{file_id}.json')

    if not os.path.exists(filepath):
        return None

    try:
        with open(filepath, 'r', encoding='utf-8') as f:
            return json.load(f)
    except json.JSONDecodeError:
        print(f"文件 {file_id}.json 格式错误")
        return None
    except Exception as e:
        print(f"读取失败: {str(e)}")
        return None